<template>
  <div class="main-container">
    <div class="login-container">
      <h2 class="title">商户登录</h2>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="login-form">
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" placeholder="请输入商户账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="ruleForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login" class="login-btn">登录</el-button>
        </el-form-item>
        <div class="register-link">
          <router-link to="/shangjia/register">还没有账号？立即注册</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入商户账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$http.post('shangjia/login', {
            shangjiazhanghao: this.ruleForm.username,
            mima: this.ruleForm.password
          }).then(({data}) => {
            if (data && data.code === 0) {
              console.log('登陆；',data)
              this.$storage.set('Token', data.token)
              this.$storage.set('shangjiaId', data.id)
              this.$storage.set('role', 'shangjia')
              this.$storage.set('sessionTable', 'shangjia')
              this.$router.push('/shangjia/center')

            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.main-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f0f2f5;
}
.login-container {
  width: 400px;
  padding: 40px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
}
.title {
  text-align: center;
  margin-bottom: 30px;
}
.login-btn {
  width: 100%;
}
.register-link {
  text-align: center;
  margin-top: 20px;
}
</style>